<?php

class Test extends Controller {

  private $testClasses;

  function Test()
	{
		parent::Controller();	
    
    $this->testClasses['usuarios'] = array('name'=>'Usuarios', 'model'=>'PrunusTblUsuarios' , 'virtual'=>FALSE);
    $this->testClasses['roles'] =    array('name'=>'Roles',    'model'=>'PrunusTblRoles'    , 'virtual'=>FALSE);
    $this->testClasses['sexos'] =    array('name'=>'(V)Sexos', 'model'=>'PrunusVTblSexos'   , 'virtual'=>TRUE );
    $this->testClasses['languages'] =array('name'=>'Languages','model'=>'PrunusTblLanguages', 'virtual'=>FALSE);
    $this->testClasses['pages'] =    array('name'=>'Pages',    'model'=>'PrunusTblPages'    , 'virtual'=>FALSE);
	}
	
	function index() {
    $out = $this->load->view('prunus_tester/header', null, true);
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>Test Root</h2>";
    $out .= "</div>";
    $out .= "<ul>";
    foreach ($this->testClasses as $objectName => $testClass) {
      $out .= "<li><a href='/test/listar/" . $objectName . "'>" . $testClass['name'] . "</a></li>";
    }
    $out .= "</ul>";
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
	}
  
	function listar($object) {
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    
    $out = $this->load->view('prunus_tester/header', null, true);
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>" . $object . "</h2>";
    $out .= "<h3>List</h3>";
    $out .= "</div>";
    $out .= "<ul>";
    $out .= "<li><a href='/test'>back to root</a></li>";
    if ( ! $this->testClasses[$object]['virtual']) $out .= "<li><a href='/test/insert/" . $object . "'>insert</a></li>";
    $out .= "</ul>";
    $out .= "<ul>";
    $out .= "<li>count=" . $oTable->countRecords() . "</li>";
    $out .= "</ul>";
    
    $out .= "<h4>alive</h4>";
    $out .= "<div class=\"listado\">";
    if ($this->testClasses[$object]['virtual']) {
      $res = $oTable->listRecords(1, null, null, null, null);
    } else {
      $res = $oTable->listRecords(1, null, null, null, null, array($oTable->getIdColumn()=>'ASC'))->result_array();
    }
    foreach ($res as $val) {
      $out .= "<div class=\"listado-renglon-area\">";
      foreach ($val as $k=>$v) {        
        $out .= "<div class=\"listado-renglon\">";
        $out .= "<span class=\"listado-renglon-titulo\">$k</span>";
        $out .= "<span class=\"listado-renglon-valor\">$v</span>";
        $out .= "<span class=\"listado-renglon-acciones\">";
        if ( $this->testClasses[$object]['virtual']) {
          if ($k == $oTable->getIdColumn()) {
            $out .= "<a href='/test/get/" . $object . "/" . $v . "'>ver</a> ";
          }
        } else {
          if ($k == $oTable->getIdColumn()) {
            $out .= "<a href='/test/get/" . $object . "/" . $v . "'>ver</a> ";
            $out .= "<a href='/test/update/" . $object . "/" . $v . "'>update</a> ";
            $out .= "<a href='/test/delete/" . $object . "/" . $v . "'>delete</a>";
          }
        }
        $out .= "</span>";
        $out .= "<div class=\"fixClear\"></div>";
        $out .= "</div>";
      }
      $out .= "</div>";
    }
    $out .= "</div>";
    
    $out .= "<h4>deleted</h4>";
    $out .= "<div class=\"listado\">";
    if ($this->testClasses[$object]['virtual']) {
      $res = $oTable->listRecords(0, null, null, null, null);
    } else {
      $res = $oTable->listRecords(0, null, null, null, null, array($oTable->getIdColumn()=>'ASC'))->result_array();
    }
    foreach ($res as $val) {
      $out .= "<div class=\"listado-renglon-area\">";
      foreach ($val as $k=>$v) {        
        $out .= "<div class=\"listado-renglon\">";
        $out .= "<span class=\"listado-renglon-titulo\">$k</span>";
        $out .= "<span class=\"listado-renglon-valor\">$v</span>";
        $out .= "<span class=\"listado-renglon-acciones\">";
        if ( ! $this->testClasses[$object]['virtual']) {
          if ($k == $oTable->getIdColumn()) {
            $out .= "<a href='/test/get/" . $object . "/" . $v . "'>ver</a> ";
            $out .= "<a href='/test/undelete/" . $object . "/" . $v . "'>undelete</a> ";
            $out .= "<a href='/test/kill/" . $object . "/" . $v . "'>kill</a>";
          }
        }
        $out .= "</span>";
        $out .= "<div class=\"fixClear\"></div>";
        $out .= "</div>";
      }
      $out .= "</div>";
    }
    $out .= "</div>";
    
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
	}

	function get($object, $id) {
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    $out = $this->load->view('prunus_tester/header', null, true);
    
    if ($this->testClasses[$object]['virtual']) {
      $res = array($oTable->getRecord($id));
    } else {
      $res = $oTable->getRecord($id)->result_array();
    }
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>" . $object . "</h2>";
    $out .= "<h3>Get id #" . $id . "</h3>";
    $out .= "</div>";
    $out .= "<ul>";
    $out .= "<li><a href='/test'>back to root</a></li>";
    $out .= "<li><a href='/test/listar/" . $object . "'>back to list</a></li>";
    if ( ! $this->testClasses[$object]['virtual']) $out .= "<li><a href='/test/update/" . $object . "/" . $id . "'>update record</a></li>";
    $out .= "</ul>";

    $out .= "<ul>";
    foreach ($res as $val) {
      foreach ($val as $k=>$v) {        
        $out .= "<li>$k = $v</li>";
      }
    } 
    $out .= "</ul>";
    
    $rel = $oTable->getRelaciones();
    foreach ($rel as $relName=>$relacion) {
      $q   = $relacion->countRecords($id);
      $res = $relacion->listRecords($id);
      $out .= "<h4>Relacion " . $relName . "</h4>";
      $out .= "<ul><li>count: " . $q . "</li>";
      $out .= "<li><a href='/test/insertrelated/" . $object . "/" . $relName . "/" . $id . "'>insert</a></li>";
      $out .= "</ul>";
      $out .= "<div class=\"listado\">";
      foreach ($res->result_array() as $val) {
        $out .= "<div class=\"listado-renglon-area\">";
        foreach ($val as $k=>$v) {        
          $out .= "<div class=\"listado-renglon\">";
          $out .= "<span class=\"listado-renglon-titulo\">$k</span>";
          $out .= "<span class=\"listado-renglon-valor\">$v</span>";
          $out .= "<span class=\"listado-renglon-acciones\">";
          if ($k == $relacion->getIdColumn()) {
            $out .= "<a href='/test/killrelated/" . $object . "/" . $relName . "/" . $id . "/" . $v . "'>kill</a>";
          }
          $out .= "</span>";
          $out .= "<div class=\"fixClear\"></div>";
          $out .= "</div>";
        }
        $out .= "</div>";
      } 
      $out .= "</div>";
    }
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
  }
  
	function insert($object) {
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    $out = $this->load->view('prunus_tester/header', null, true);
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>" . $object . "</h2>";
    $out .= "<h3>Insert new record</h3>";
    $out .= "</div>";
    $out .= "<ul>";
    $out .= "<li><a href='/test'>back to root</a></li>";
    $out .= "<li><a href='/test/listar/" . $object . "'>back to list</a></li>";
    $out .= "</ul>";

    if ($this->input->post("tag") == "tag") {
      $this->load->helper('url');
      foreach ($_POST as $k=>$v) {
        if ($k != "tag") {
          $arrayInsert[$k] = $v;
        }
      }
      $newId = $oTable->insertRecord($arrayInsert);
      redirect('/test/get/' . $object . "/" . $newId, 'location', 301);
    } else {
      $out .= "<div class=\"form\">";
      $out .= "<form method=\"post\">";
      $out .= "<input type=\"hidden\" name=\"tag\" value=\"tag\" />";
      $insertColumns = $oTable->getInsertColumns();
      foreach ($insertColumns as $insertColumn) {
        $out .= "<div class=\"form-row\">";
        $out .= "<span class=\"form-row-titulo\">";
        $out .= $insertColumn;
        $out .= "</span>";
        $out .= "<span class=\"form-row-valor\">";
        $out .= "<input type=\"text\" name=\"" . $insertColumn . "\" value=\"" . $this->input->post($insertColumn) . "\" />";
        $out .= "</span>";
        $out .= "</div>";
      }
      $out .= "<input type=\"submit\">";
      $out .= "</form>";
      $out .= "</div>";
    }
    
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
  }
  
	function update($object, $id) {
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    $out = $this->load->view('prunus_tester/header', null, true);
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>" . $object . "</h2>";
    $out .= "<h3>Update id #" . $id . "</h3>";
    $out .= "</div>";
    $out .= "<ul>";
    $out .= "<li><a href='/test'>back to root</a></li>";
    $out .= "<li><a href='/test/listar/" . $object . "'>back to list</a></li>";
    $out .= "</ul>";
    
    if ($this->input->post("tag") == "tag") {
      $this->load->helper('url');
      foreach ($_POST as $k=>$v) {
        if ($k != "tag") {
          $arrayUpdate[$k] = $v;
        }
      }
      $oTable->updateRecord($id,$arrayUpdate);
      redirect('/test/get/' . $object . "/" . $id, 'location', 301);
    } else {
      $out .= "<div class=\"form\">";
      $out .= "<form method=\"post\">";
      $out .= "<input type=\"hidden\" name=\"tag\" value=\"tag\" />";
      $updateColumns = $oTable->getUpdateColumns();
      $res = $oTable->getRecord($id)->result_array();
      foreach ($res as $val) {
        foreach ($val as $k=>$v) {        
          if (in_array($k, $updateColumns)) {
            $value = $this->input->post($k) !== FALSE ? $this->input->post($k) : $v;
            $out .= "<div class=\"form-row form-row-readonly\">";
            $out .= "<span class=\"form-row-titulo\">";
            $out .= $k;
            $out .= "</span>";
            $out .= "<span class=\"form-row-valor\">";
            $out .= "<input type=\"text\" name=\"" . $k . "\" value=\"" . $value . "\" />";
            $out .= "</span>";
            $out .= "</div>";
          } else {
            $out .= "<div class=\"form-row form-row-readonly\">";
            $out .= "<span class=\"form-row-titulo\">";
            $out .= $k;
            $out .= "</span>";
            $out .= "<span class=\"form-row-valor\">";
            $out .= $v;
            $out .= "</span>";
            $out .= "</div>";
          }
        }
      } 
      $out .= "<input type=\"submit\">";
      $out .= "</form>";
      $out .= "</div>";
    }
    
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
  }
  
	function delete($object, $id) {
    $this->load->helper('url');
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    if ($oTable->deleteRecord($id)) redirect('/test/listar/' . $object, 'location', 301);
  }
  
	function kill($object, $id) {
    $this->load->helper('url');
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    if ($oTable->killRecord($id)) redirect('/test/listar/' . $object, 'location', 301);
  }
  
	function undelete($object, $id) {
    $this->load->helper('url');
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    if ($oTable->undeleteRecord($id)) redirect('/test/listar/' . $object, 'location', 301);
  }
  
	function killrelated($object, $relation, $objectId, $relationId) {
    $this->load->helper('url');
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    $rel = $oTable->getRelacion($relation);
    if ($rel->killRecord($relationId)) redirect('/test/get/' . $object . "/" . $objectId, 'location', 301);
  }
  
	function insertrelated($object, $relation, $objectId) {
    $this->load->model($this->testClasses[$object]['model'],'objeto');
    $oTable = $this->objeto;
    $rel = $oTable->getRelacion($relation);
    $out = $this->load->view('prunus_tester/header', null, true);
    $out .= "<div id=\"header\">";
    $out .= "<h1>Prunus Test</h1>";
    $out .= "<h2>" . $object . "</h2>";
    $out .= "<h3>Insert new record on relation $relation</h3>";
    $out .= "</div>";
    $out .= "<ul>";
    $out .= "<li><a href='/test'>back to root</a></li>";
    $out .= "<li><a href='/test/get/" . $object . "/" . $objectId . "'>back to record</a></li>";
    $out .= "</ul>";
    
    
    if ($this->input->post("tag") == "tag") {
      $this->load->helper('url');
      foreach ($_POST as $k=>$v) {
        if ($k != "tag") {
          $arrayInsert[$k] = $v;
        }
      }
      $newId = $rel->insertRecord($objectId, $arrayInsert);
      redirect('/test/get/' . $object . "/" . $objectId, 'location', 301);
    } else {
      $out .= "<div class=\"form\">";
      $out .= "<form method=\"post\">";
      $out .= "<input type=\"hidden\" name=\"tag\" value=\"tag\" />";

      $insertColumns = $rel->getInsertColumns();
      foreach ($insertColumns as $insertColumn) {
        $out .= "<div class=\"form-row\">";
        $out .= "<span class=\"form-row-titulo\">";
        $out .= $insertColumn;
        $out .= "</span>";
        $out .= "<span class=\"form-row-valor\">";
        
        $res = $rel->listInsertableRecords($insertColumn, $objectId);
        if (count($res) == 0) {
          $out .= "<input type=\"text\" name=\"" . $insertColumn . "\" value=\"" . $this->input->post($insertColumn) . "\" />";
        } else {
          $res = $res->result_array();
          $out .= "<select name=\"" . $insertColumn . "\">";
          foreach ($res as $val) {
            $out .= "<OPTION VALUE=\"" . $val[$rel->getInsertableRecordIdColumnName()] . "\">" . $val[$rel->getInsertableRecordValueColumnName()] . "</OPTION>";
          }
          $out .= "</select>";
        }

        $out .= "</span>";
        $out .= "</div>";
      }
      
      
      
      
      
      
      
      

      $out .= "<input type=\"submit\">";
      $out .= "</form>";
      $out .= "</div>";
    }
    
    $out .= $this->load->view('prunus_tester/footer', null, true);
    echo $out;
  }
  
  
  
}


/* End of file test.php */
/* Location: ./system/application/controllers/test.php */